package org.helldorado.companybuilder;

import android.app.Activity;
import android.support.v4.app.Fragment;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;

import org.helldorado.companybuilder.R;
//import org.helldorado.companybuilder.ModelListFragment.OnItemSelectedListener;

// extended from compatibility Fragment for pre-HC fragment support
public class ModelListFragment extends Fragment {
   
   // views
   Button btn1,btn2;
   
   // activity listener
   private OnModelClickListener modelClickListener;

   // interface for communication with activity
   public interface OnModelClickListener {
      public void onModelClickListener(Model s);
   }
   
   // onAttach
   @Override
   public void onAttach(Activity activity) {
      super.onAttach(activity);
      try {
    	  modelClickListener = (OnModelClickListener) activity;
      } catch (ClassCastException e) {
         throw new ClassCastException(activity.toString()+" must implement OnModelClickListener");
      }
   }
   
   // onCreate
   @Override
   public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
   }

   // onActivityCreated
   @Override
   public void onActivityCreated(Bundle savedInstanceState) {
      super.onActivityCreated(savedInstanceState);
   }
   
   // onCreateView
   @Override
   public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState) {
//      View view = inflater.inflate(R.layout.fr_menu,container,false);
//      
//      // get button BTN1
//      btn1 = (Button)view.findViewById(R.id.btn1);
//      // button listener
//      btn1.setOnClickListener(new View.OnClickListener() {
//         @Override
//         public void onClick(View v) {
//            sendBodyTextToFragment("Text From Fragment");
//         }
//      });
//      
//      // get button BTN2
//      btn2 = (Button)view.findViewById(R.id.btn2);
//      // button listener
//      btn2.setOnClickListener(new View.OnClickListener() {
//         @Override
//         public void onClick(View v) {
//            sendBodyTextToActivity("Text From Activity");
//         }
//      });
	   View view = inflater.inflate(R.layout.fr_menu,
		        container, false);
			  Activity act = getActivity();
			  Context ctx = (Context)act;
			  ModelListAdapter mAdapter = new ModelListAdapter(ctx, -1, ModelList.getInstance(ctx).getList());
			  ListView modelList = (ListView)view.findViewById(R.id.modelList);
			  modelList.setAdapter(mAdapter);
			  modelList.setOnItemClickListener(new OnItemClickListener() {
				@Override
				public void onItemClick(AdapterView<?> parent, View v, int pos,long id) {
					modelClickListener.onModelClickListener((Model)parent.getAdapter().getItem(pos));
				}});
      return view;
   }
   
   // alternate (not recommended) method with direct access to fragment
//   private void sendModelToFragment(Model m) {
//      
//      // get body fragment (native method is getFragmentManager)
//      BodyFragment fragment = (BodyFragment) getActivity().getSupportFragmentManager().findFragmentById(R.id.bodyFragment);
//      
//      // if fragment is not null and in layout, set text, else launch BodyActivity
//      if ((fragment!=null)&&fragment.isInLayout()) {
//         fragment.setText(m);
//      } else {
//         Intent intent = new Intent(getActivity().getApplicationContext(),BodyActivity.class);
//         intent.putExtra("value",m);
//         startActivity(intent);
//      }
//      
//   }
   
}
/*public class ModelListFragment extends Fragment {
	  
	  private OnItemSelectedListener listener;
	  //private List<>
	  
	  @Override
	  public View onCreateView(LayoutInflater inflater, ViewGroup container,
	      Bundle savedInstanceState) {

		  View view = inflater.inflate(R.layout.modellist_fragment,
	        container, false);
		  Activity act = getActivity();
		  Context ctx = (Context)act;
		  SitesAdapter mAdapter = new SitesAdapter(ctx, -1, SitesXmlPullParser.getModelsFromFile(ctx));
		  ListView sitesList = (ListView)view.findViewById(R.id.sitesList);
	    sitesList.setAdapter(mAdapter);
	    //Button button = (Button) view.findViewById(R.id.button2);
	    //button.setOnClickListener(new View.OnClickListener() {
	    //  @Override
	    //  public void onClick(View v) {
	    //    updateDetail();
	    //  }
	    //});
	    return view;
	  }

	  public interface OnItemSelectedListener {
	      public void onRssItemSelected(String link);
	    }
	  
	  @Override
	    public void onAttach(Activity activity) {
	      super.onAttach(activity);
	      if (activity instanceof OnItemSelectedListener) {
	        listener = (OnItemSelectedListener) activity;
	      } else {
	        throw new ClassCastException(activity.toString()
	            + " must implemenet MyListFragment.OnItemSelectedListener");
	      }
	    }
	  
	  
	  // May also be triggered from the Activity
	  public void updateDetail() {
	    // create fake data
	    String newTime = String.valueOf(System.currentTimeMillis());
	    // Send data to Activity
	    listener.onRssItemSelected(newTime);
	  }
	} 
*/